<?php

/**
 *
 * @copyright 2011 by Dan, Steven, Brice, Mike, and Aaron.  This program is part
 * of Internship 2 Career, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version April 10, 2011
 *
 * @author Daniel pepin
 */
checkSortBy();

$result = getOpportunities();

drawHeader();

drawList($result);

///////////// FUNCTIONS //////////////////////////////////////////////////////////


function checkSortBy() {
   if (isset($_REQUEST['sortBy'])) {
      $_SESSION['sortBy'] = $_REQUEST['sortBy'];
   }
}

/**
 * Gets all the opportunities from the database.
 * @return object Return MySQL object of all opportunites
 */
function getOpportunities() {
   include 'db-control/dbOpportunity.php';
   return dbOpportunity::getAllOpportunities();
}

/**
 * Draws the column headers.
 */
function drawHeader() {
   echo "<table width='100%' cellspacing=0>
      <tr>
         <td width='3%'></td>
         <td width='10%'><b>Company</b>
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Descending' value='org_asc' name='sortBy' src='img/arrow_down.png' />
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Ascending' value='org_desc' name='sortBy' src='img/arrow_up.png' />
         </td>
         <td width='10%'><b>Location</b>
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Descending' value='city_asc' name='sortBy' src='img/arrow_down.png' />
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Ascending' value='city_desc' name='sortBy' src='img/arrow_up.png' />
         </td>
         <td width='10%'><b>Position</b>
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Descending' value='pos_asc' name='sortBy' src='img/arrow_down.png' />
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Ascending' value='pos_desc' name='sortBy' src='img/arrow_up.png' />
         </td>
         <td width='10%'><b># Available</b>
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Descending' value='numAvailable_asc' name='sortBy' src='img/arrow_down.png' />
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Ascending' value='numAvailable_desc' name='sortBy' src='img/arrow_up.png' />  
         </td>
         <td width='10%'><b>Semester</b>
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Descending' value='sem_asc' name='sortBy' src='img/arrow_down.png' />
            <input type='image' style='background:none;border:0;width:20px;height:10px;' title='Sort Ascending' value='sem_desc' name='sortBy' src='img/arrow_up.png' />         
         </td>
      </tr>";

   echo "<tr><td colspan=8><hr /></td></tr>";
}

/**
 * Draws the list of opportunities.
 * @param object $result MySQL object
 */
function drawList($result) {

   $numRows = mysql_num_rows($result);

   for ($x = 0; $x < $numRows; $x++) {
      $row = mysql_fetch_assoc($result);
      $id = $row['opportunityID'];
      $city = $row['city'];
      $state = strtoupper($row['stateName']);
      $title = $row['title'];
      $name = $row['organizationName'];
      $numPositions = $row['numPositionsAvailable'];
      $payRate = $row['payRate'];
      $semester = $row['semester'];
      $dateAdded = $row['dateAdded'];
      $dateAdded = substr($dateAdded, 0, 10);
      if ($x % 2 == 0) {
         echo "<tr>";
      } else {
         echo "<tr bgcolor='#FBFBFB'>";
      }
      echo "<td align='center' valign='middle'><br /><form action='index.php' method='get'><input type='hidden' name='internship_id' value='$id'><input type='submit' value='View' class='submit' name='view_info' /></form></td>";
      echo "<td>$name</td>";
      echo "<td>$city, " . $state . "</td>";
      echo "<td>$title</td>";
      echo "<td>$numPositions</td>";
      echo "<td>$semester</td>";
      echo "</tr>";
   }
   echo "</table>";
}

?>
